package com.service.bank.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.client.model.pojo.bank.ArBankcard;
import com.client.model.pojo.bank.Deposit;
import com.client.model.pojo.bank.DepositRecord;
import com.client.model.pojo.bank.DepositType;
import com.client.model.vo.common.Result;
import com.client.model.vo.common.ResultCodeEnum;
import com.client.model.vo.deposit.DepositVo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.service.bank.mapper.DepositRecordMapper;
import com.service.bank.service.ArBankcardService;
import com.service.bank.service.DepositService;
import com.service.bank.mapper.DepositMapper;
import com.service.bank.service.DepositTypeService;
import org.apache.commons.codec.digest.Md5Crypt;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author liuwenju
* @description 针对表【deposit】的数据库操作Service实现
* @createDate 2024-04-01 20:55:27
*/
@Service
public class DepositServiceImpl extends ServiceImpl<DepositMapper, Deposit>
    implements DepositService{

    @Autowired
    private DepositMapper depositMapper;

    @Autowired
    private DepositTypeService depositTypeService;

    @Autowired
    private ArBankcardService arBankcardService;

    @Autowired
    private DepositRecordMapper depositRecordMapper;



    @Override
    public Result findDeposToType(String bankCard) {
        //两表查询返回需要的字段
        List<DepositVo> depositVos = depositMapper.selectJoinList(DepositVo.class,
                new MPJLambdaWrapper<Deposit>()
                        .selectAll(Deposit.class)
                        .selectAll(DepositType.class)
                        .innerJoin(DepositType.class, DepositType::getId, Deposit::getDid)
                        .eq("is_type", 0)
                        .eq("bank_card", bankCard));
       if (depositVos.size() ==0){
           return Result.build(null, 500,"改卡号活期未存在存款");
       }
        return Result.build(depositVos, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result currentTofixed(String bankCard, Long did, BigDecimal price) {
        //根据卡号查找出该用户的活期存款
        DepositType depositType = depositTypeService.getOne(new QueryWrapper<DepositType>().eq("is_type", 0));
        Deposit bankCard1 = depositMapper.selectOne(new QueryWrapper<Deposit>()
                .eq("bank_card", bankCard).eq("did", depositType.getId()));
        //如果为空说明改用户没有存过钱
        if (bankCard1 == null) {
            return Result.build(null, 500, "卡号尚未存款活期");
        }
        //余额不足
        if (bankCard1.getBalance().compareTo(price) < 0) {
            System.err.println("余额不足");
            return Result.build(null, 500, "余额不足");
        }
        //余额充足
        //原有金额加上转为定期金额的利息
        //活期率为固定0.35%
        bankCard1.setBalance(bankCard1.getBalance().add(price.multiply(new BigDecimal("0.0035"))));
        //保存减去后金额数据
        depositMapper.updateById(bankCard1);
        //查询改卡号是否存在定期存款 是否为同一天
        List<DepositType> isType = depositTypeService.list(new QueryWrapper<DepositType>().eq("is_type", 1));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Deposit> fied = new ArrayList<>();
        for (DepositType m : isType) {
            List<Deposit> deposits = depositMapper.selectList(new QueryWrapper<Deposit>()
                    .eq("bank_card", bankCard).eq("did", m.getId()));
            for (Deposit deposit : deposits){
                fied.add(deposit);
            }
        }
        //如果定期存款不为空
        //查看存款定期日期中有没有为一天的 && 存款日期长短也相等
        for (Deposit deposit : fied) {
            if (sdf.format(deposit.getCreateTime()).equals(sdf.format(new Date()))
               && deposit.getDid()==did) {
                //则定期金额加上原有金额
                deposit.setBalance(deposit.getBalance().add(price));
                //保存定期金额
                depositMapper.updateById(deposit);
                return Result.build(null, ResultCodeEnum.SUCCESS);
                }
            }
                //如果存款定期日期不为一天
                //则创建一个新的定期存款
                Deposit deposit = new Deposit();
                deposit.setBankCard(bankCard);
                deposit.setDid(did);
                deposit.setBalance(price);
                depositMapper.insert(deposit);
                return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result fixedToCurrent(String bankCard,Long depositid, BigDecimal price) {
        //根据卡号查找出该用户的定期存款
//        DepositType depositType = depositTypeService.getOne(new QueryWrapper<DepositType>().eq("is_type", 1).eq("id",typeid));
        Deposit bankCard1 = depositMapper.selectOne(new QueryWrapper<Deposit>()
                .eq("bank_card", bankCard)
                .eq("id",depositid));
        //如果为空说明改用户没有存过钱
        if (bankCard1 == null) {
            return Result.build(null, 500, "卡号尚未存款活期");
        }
        //余额不足
        if (bankCard1.getBalance().compareTo(price) < 0) {
            System.err.println("余额不足");
            return Result.build(null, 500, "余额不足");
        }
        //原有金额加上转为定期金额的利息
        //活期率为固定0.35%
        bankCard1.setBalance(bankCard1.getBalance().add(price.multiply(new BigDecimal("0.0035"))));
        //保存原有余额加上利息
        depositMapper.updateById(bankCard1);
        //把转出的余额存放到活期里面
        DepositType type = depositTypeService.getOne(new QueryWrapper<DepositType>().eq("is_type", 0));
        Deposit bank = depositMapper.selectOne(new QueryWrapper<Deposit>()
                .eq("bank_card", bankCard).eq("did", type.getId()));
        if (bank == null){
            //说明该用户不存在活期
            bank = new Deposit();
            bank.setBankCard(bankCard);
            bank.setDid(type.getId());
            bank.setBalance(price);
            depositMapper.insert(bank);
            return Result.build(null,200,"申请成功");
        }
        bank.setBalance(bank.getBalance().add(price));
        //保存
        depositMapper.updateById(bank);
        return Result.build(null,200,"申请成功");
    }

    @Override
    public Result confirm(String bankCard, Long did, BigDecimal price,String payPassword) {
        System.err.println(did);
        //判断支付密码是否正确
        //利用卡号拿到盐
        ArBankcard backCard = arBankcardService.getOne(new QueryWrapper<ArBankcard>().eq("bank_card", bankCard));
        if (backCard == null) {
            return Result.build(null, 500, "卡号不存在");
        }
        //利用盐和密码进行加密
        String s = DigestUtils.md5DigestAsHex((payPassword + backCard.getSalt()).getBytes());
        if (!s.equalsIgnoreCase(backCard.getPayPassword())){
            return Result.build(null, 500, "支付密码错误");
        }
        //将该卡号活期存款金额减去
        Deposit deposit = depositMapper.selectOne(new QueryWrapper<Deposit>().eq("bank_card", bankCard).eq("did",6));
        deposit.setBalance(deposit.getBalance().subtract(price));
        depositMapper.updateById(deposit);
        //添加到申请管理表中
        DepositRecord depositRecord = new DepositRecord();
        depositRecord.setBankCard(bankCard);
        depositRecord.setBalance(price);
        depositRecord.setIsType(did);
        depositRecord.setRemark(deposit.getId());
        depositRecordMapper.insert(depositRecord);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result confirmfixed(String bankCard, Long depositid, BigDecimal price, String payPassword) {
        //判断支付密码是否正确
        //利用卡号拿到盐
        ArBankcard backCard = arBankcardService.getOne(new QueryWrapper<ArBankcard>().eq("bank_card", bankCard));
        if (backCard == null) {
            return Result.build(null, 500, "卡号不存在");
        }
        //利用盐和密码进行加密
        String s = DigestUtils.md5DigestAsHex((payPassword + backCard.getSalt()).getBytes());
        if (!s.equalsIgnoreCase(backCard.getPayPassword())){
            return Result.build(null, 500, "支付密码错误");
        }
        //申请的卡号的余额是否足够
        System.err.println("id"+depositid);
        Deposit deposit1 = depositMapper.selectOne(new QueryWrapper<Deposit>().eq("bank_card", bankCard).eq("id", depositid));
        if (deposit1.getBalance().compareTo(price)<0){
            System.out.println(backCard.getBalance().compareTo(price));
            return Result.build(null, 500, "余额不足");
        }
        //将该卡号活期存款金额减去
        Deposit deposit = depositMapper.selectOne(new QueryWrapper<Deposit>().eq("bank_card", bankCard).eq("id",depositid));
        deposit.setBalance(deposit.getBalance().subtract(price));
        depositMapper.updateById(deposit);
        //添加到申请管理表中
        DepositRecord depositRecord = new DepositRecord();
        depositRecord.setBankCard(bankCard);
        depositRecord.setBalance(price);
        depositRecord.setIsType(1L);
        depositRecord.setRemark(deposit.getId());
        depositRecordMapper.insert(depositRecord);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result findDeposToFxiedType(String bankCard) {
        //两表查询返回需要的字段
        List<DepositVo> depositVos = depositMapper.selectJoinList(DepositVo.class,
                new MPJLambdaWrapper<Deposit>()
                        .selectAll(Deposit.class)
                        .selectAll(DepositType.class)
                        .innerJoin(DepositType.class, DepositType::getId, Deposit::getDid)
                        .eq("is_type", 1)
                        .eq("bank_card", bankCard));
        if (depositVos.size() ==0){
            return Result.build(null, 500,"改卡号定期未有存款");
        }
        return Result.build(depositVos, ResultCodeEnum.SUCCESS);
    }

}




